package com.cheung.leetcode.str;

public class LC58 {

    public static int lengthOfLastWord(String s) {
        int i = s.length() - 1;
        // 如果最后一个字符是空格
        if (s.charAt(s.length() - 1) == ' ') {
            while (s.charAt(i) == ' ') {
                i--;
            }
        }

        int right = i;

        while (i >= 0 && s.charAt(i) != ' ') {
            i--;
        }

        return right - i;

    }

    public static void main(String[] args) {
        String s = "luffy is still joyboy";
        System.out.println(lengthOfLastWord(s));
    }
}
